<?php

//git webhook 自动部署脚本
//项目存放物理路径,第一次clone时,必须保证该目录为空
$savePath = "D:\wwwroot\dr.yn123.com\wwwroot";
$gitPath  = "https://gitee.com/finalove/dongrun";//代码仓库
$email = "18388400315@163.com";//用户仓库邮箱
$name  = "finalove";//仓库用户名,一般和邮箱一致即可

//注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码
shell_exec("git config --global user.email {$email}}");
shell_exec("git config --global user.name {$name}}");
$requestBody = file_get_contents("php://input");
$content = json_decode($requestBody, true);
$res = PHP_EOL;
//$res .= ">>>>>>>>>>>>>>>>>[".date('Y-m-d H:i:s')."]<<<<<<<<<<<<<<<".PHP_EOL;
//$res .= $content['user_name'] .' 向 ' . $content['repository']['name'];
//$res .= ' 项目的 ' . $content['ref'] . ' 分支push了 ' . $content['total_commits_count'] . ' 个commit：' . PHP_EOL;
// $res .= shell_exec("git clone {$gitPath} {$savePath}").PHP_EOL;
//$res .= shell_exec("cd {$savePath} && git pull {$gitPath}");//拉去代码
//$res .= "<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>".PHP_EOL;
//$res = iconv("utf-8","gb2312//IGNORE",$res);
//file_put_contents("git-webhook_log.txt", $res, FILE_APPEND);//写入日志到log文件中

$res .= "<h3 style='color:#09F;font-family: '微软雅黑'>".date('Y-m-d H:i:s')."</h3>".PHP_EOL;
$res .= "<p style='color:#3E3D3F';font-family: '微软雅黑'><span style='color:#56A36C'>".$content['user_name'] .'</span>向 ' . $content['repository']['name'];
$res .= ' 项目的 ' . $content['ref'] . ' 分支push了 ' . $content['total_commits_count'] . ' 个commit：</p>' . PHP_EOL;
// $res .= shell_exec("git clone {$gitPath} {$savePath}").PHP_EOL;
$res .= "<h4 style='background-color:#495A80;padding:5px;color:#FFF'>".shell_exec("cd {$savePath} && git pull {$gitPath}")."</h4>";//拉去代码
$res .= "<hr>".PHP_EOL;
$res = iconv("utf-8","gb2312//IGNORE",$res);
file_put_contents("git_log.html", $res, FILE_APPEND);//写入日志到log文件中
